const express=require('express');
const db=require('../utils/db');
const upload=require('../utils/multer')
const route=express.Router();

//运行函数
function  dbquery(sql,res){
    db.query(sql,function(err,data){
        if(err){
            res.send({'code':500,msg:err})
        }else{
            res.send({'code':200,data:data})
        }
    })
}
//登录接口
route.post('/login',function(req,res){
    let {u_name,u_pass}=req.body;

    let sql=`select * from t_user where u_name='${u_name}' and u_pass='${u_pass}';`;;
    
    db.query(sql,function(err,data){
        if(err){
            res.send({'code':500,msg:err})
        }else{
            if(data.length>0){
                res.send({'code':200,data:data[0],msg:'登录成功'})
            }else{
                res.send({'code':400,msg:'登录失败'})
            }
        }
    })
})
//获取区
route.get('/area',function(req,res){

    let sql=`select * from t_area;`;
    
    dbquery(sql,res)
})
//获取街道
route.get('/street',function(req,res){
    let {a_id}=req.query;
    let sql=`select * from t_street where s_a_id=${a_id};`;
    
    dbquery(sql,res)
})
//获取小区
route.get('/estate',function(req,res){
    let {s_id}=req.query;
    let sql=`select * from t_estate where e_s_id=${s_id};`;
    
    dbquery(sql,res)
})
//获取住户
route.get('/household',function(req,res){
    let {e_id}=req.query;
    let sql=`select * from t_household where h_e_id=${e_id};`;
    
    dbquery(sql,res)
})
//获取某小区的住户本月是否检查过
route.get('/monthIsCheck',function(req,res){
    let {e_id,year,month}=req.query;
    let sql=`select * from t_patrol where p_h_id=${e_id} and p_year='${year}' and p_month='${month}';`;
    
    dbquery(sql,res)
})

//获取住户的巡检信息  //获取住户年度巡检信息
route.get('/userCheck',function(req,res){
    let {h_id,year}=req.query;

    if(year){
        var  sql=`select * from t_patrol where p_h_id=${h_id} and p_year=${year};`;
    }else{
        var  sql=`select * from t_patrol where p_h_id=${h_id};`;
    }
    
    
    dbquery(sql,res)
})

//添加信息
route.post('/addCheckMsg',function(req,res){
    let {year,month,day,u_id,h_id,num,isleakage,safe,img}=req.body;
    let sql=`insert into t_patrol values(null,'${year}','${month}','${day}',${num},'${safe}','${isleakage}','${img}',${h_id},${u_id})`;
    
    dbquery(sql,res)
})
//添加图片
route.post('/updataImg',upload.single('img'),function(req,res){
    const path=req.file.path;
    if(path){
        if(req.file.mimetype.split('/')[0]!='image'){
            res.send({code:400,msg:'上传的不是图片'})
        }else{
            res.send({code:200,img:path.split('\\').slice(1).join('/')})
        }
    }else{
        res.send({code:400,msg:'上传失败'})
    }
})
//查看某小区时候今天已经签到过了
route.get('/isClockIn',function(req,res){
    let {u_id,e_id,year,month,day}=req.query;
    let sql=`select * from t_record where r_year='${year}' and r_month='${month}' and r_day='${day}' and r_e_id=${u_id} and r_u_id=${e_id}`;
    
    db.query(sql,function(err,data){
        if(err){
            res.send({'code':500,msg:err})
        }else{
            if(data.length>0){
                res.send({'code':200,data:data[0],msg:'已经签到'})
            }else{
                res.send({'code':400,msg:'没有签到'})
            }
        }
    })
})
//打卡
route.post('/clockIn',function(req,res){
    let {u_id,e_id,year,month,day}=req.body;
    let sql=`insert into  t_record values(null,'${year}','${month}','${day}',${e_id},${u_id})`;
    
    dbquery(sql,res)
})
module.exports=route;